package wc.model.screen;

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.IOException;

import javax.imageio.ImageIO;

import wc.MallApplication;

public class Screen {
	private int width;
	private int height;
	private Bitmap canvas;
	
	BufferedImage image;
	
	public Screen(int width, int height) {
		this.width = width;
		this.height = height;
		canvas = new Bitmap(width, height);
		
		image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		canvas.setPixels(((DataBufferInt) image.getRaster().getDataBuffer()).getData());
	}

	public Bitmap[][] cut(String texture, int width, int height) {
		return cut(texture, width, height, 0, 0);
	}
	
	public Bitmap[][] cut(String texture, int width, int height, int startX, int startY) {
		try {
			BufferedImage bufferedImage = ImageIO.read(MallApplication.class.getResource(texture));
			
			int tilesX = (bufferedImage.getWidth() - startX) / width;
			int tilesY = (bufferedImage.getHeight() - startY) / height;
			
			Bitmap[][] result = new Bitmap[tilesX][tilesY];
			
			for(int x = 0; x < tilesX; x++) {
				for(int y = 0; y < tilesY; y++) {
					result[x][y] = new Bitmap(width, height);
					bufferedImage.getRGB(startX + x * width, startY + y * height, width, height, result[x][y].getPixels(), 0, width);
				}
			}
			
			return result;
		} catch(IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void blit(Bitmap bitmap, int startX, int startY) {
		Rectangle rectangle = new Rectangle(bitmap.getWidth(), bitmap.getHeight(), startX, startY);
	
		int blitWidth = rectangle.getBottomRightX() - rectangle.getTopLeftX();
	
		for (int y = rectangle.getTopLeftY(); y < rectangle.getBottomRightY(); y++) {
			int tp = y * canvas.getWidth() + rectangle.getTopLeftX();
			int sp = (y - startY) * bitmap.getWidth()+ (rectangle.getTopLeftX() - startX);
				
			tp -= sp;
			for (int x = sp; x < sp + blitWidth; x++) {
				int column = bitmap.getPixelsByIndex(x);
				int alpha = (column >> 24) & 0xff;
			
				if(alpha == 255) {
					canvas.setPixels(tp + x, column);
				} else {
					canvas.setPixels(tp + x, blendPixels(canvas.getPixelsByIndex(tp + x), column));
				}
				
			}
		}
	}
	
	private int blendPixels(int backgroundColor, int pixelToBlendColor) {
		int alphaBlend = (pixelToBlendColor >> 24) & 0xff;
		
		int alphaBackground = 256 - alphaBlend;
		
		int rr = backgroundColor & 0xff0000;
		int gg = backgroundColor & 0xff00;
		int bb = backgroundColor & 0xff;
		
		int r = (pixelToBlendColor & 0xff0000);
		int g = (pixelToBlendColor & 0xff00);
		int b = (pixelToBlendColor & 0xff);
		
		r = ((r * alphaBlend + rr * alphaBackground) >> 8) & 0xff0000;
		g = ((g * alphaBlend + gg * alphaBackground) >> 8) & 0xff00;
		b = ((b * alphaBlend + bb * alphaBackground) >> 8) & 0xff;
		
		return 0xff000000 | r | g | b;
	}
	
	public BufferedImage getImage() {
		return image;
	}
}
